
import Vue from 'vue'
import VueRouter from 'vue-router';
import routes from './routes';
// 本质在调用插件内部的install方法,只有调用它,插件功能才会添加到Vue身上和Vm身上
Vue.use(VueRouter)

const originPush = VueRouter.prototype.push  //  把原本的push函数地址存起来,防止后面要用
const originReplace = VueRouter.prototype.replace  //  把原本的replace函数地址存起来,防止后面要用
//   把今后使用的push改成我们自己定义的
VueRouter.prototype.push = function (location, resolved, rejected) {
    if (resolved === undefined && rejected === undefined) {
        return originPush.call(this, location).catch(() => { })
    } else {
        return originPush.call(this, location, resolved, rejected)
    }
}
VueRouter.prototype.replace = function (location, resolved, rejected) {
    if (resolved === undefined && rejected === undefined) {
        return originReplace.call(this, location).catch(() => { })
    } else {
        return originReplace.call(this, location, resolved, rejected)
    }
}

// 必须往外暴露一个路由器对象
const router = new VueRouter({
    routes,
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 }
    }
})


export default router
// 必须在Vue的配置对象总注册路由器对象